<template>
  <div class="department-container">
    <div class="app-container">
      <el-card>
        <!-- 用一个行列布局 -->
        <el-row type="flex" justify="space-between" align="middle" style="height: 40px">
          <el-col :span="20">
            <svg-icon icon-class="home" /><span>江苏传智播客教育科技股份有限公司</span>
          </el-col>
          <el-col :span="4">
            <el-row type="flex" justify="end">
              <!-- 两个内容 -->
              <el-col>负责人</el-col>
              <!-- 下拉菜单 element -->
              <el-col>
                <el-dropdown>
                  <span>
                    操作<i class="el-icon-arrow-down" />
                  </span>
                  <el-dropdown-menu slot="dropdown">
                    <!-- 一级部门的 pid 都为空 -->
                    <el-dropdown-item @click.native="hAdd('')">添加子部门</el-dropdown-item>
                  </el-dropdown-menu>
                </el-dropdown>
              </el-col>
            </el-row>
          </el-col>
        </el-row>
        <el-tree :data="list" :default-expand-all="true">
          <template v-slot="{data}">
            <el-row type="flex" justify="space-between" align="middle" style="height: 40px; width: 100%;">
              <el-col :span="20">
                <svg-icon icon-class="home" /><span>{{ data.name }}</span>
              </el-col>
              <el-col :span="4">
                <el-row type="flex" justify="end">
                  <!-- 两个内容 -->
                  <el-col>{{ data.manager }}</el-col>
                  <!-- 下拉菜单 element -->
                  <el-col>
                    <el-dropdown>
                      <span>
                        操作<i class="el-icon-arrow-down" />
                      </span>
                      <el-dropdown-menu slot="dropdown">
                        <el-dropdown-item @click.native="hAdd(data.id)">添加子部门</el-dropdown-item>
                        <el-dropdown-item @click.native="hEdit(data.id)">编辑部门</el-dropdown-item>
                        <!-- 如果当前部门有子部门,不提供删除功能 -->
                        <el-dropdown-item v-if="data.children.length===0" @click.native="del(data.id)">删除部门</el-dropdown-item>
                      </el-dropdown-menu>
                    </el-dropdown>
                  </el-col>
                </el-row>
              </el-col>
            </el-row>
          </template>
        </el-tree>
        <el-dialog :close-on-press-escape="false" :close-on-click-modal="false" :title="isEdit?'编辑':'添加'" :visible.sync="showDialog">
          <AddorEdit v-if="showDialog" :id="curId" :is-edit="isEdit" @success="hsuccess" @close="showDialog=false" />
        </el-dialog>
      </el-card>
    </div>
  </div>
</template>
<script>
import { getDepartments, delDepartments } from '@/api/departments'
import { tranListToTreeData } from '@/utils'
import AddorEdit from './deptDialog.vue'
export default {
  components: {
    AddorEdit
  },
  data() {
    return {
      // 依赖一份树形数据
      list: [],
      showDialog: false,
      // 当前点击的id
      curId: '',
      isEdit: false

    }
  },
  created() {
    // 调用获取部门详情的接口
    this.getDepartments()
  },
  methods: {
    del(id) {
      this.$confirm('你真的要删除吗?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(async() => {
        // console.log(id)
        const res = await delDepartments(id)
        // console.log(res)
        // 提醒用户
        this.$message.success(res.message)
        // 关闭弹框
        this.showDialog = false
        // 重新渲染页面
        this.getDepartments()
      }).catch(() => {
        // 不做任何操作
      })
    },
    hsuccess() {
      // 父组件自定义事件,触发回调
      // 关闭弹层,重新渲染
      this.showDialog = false
      this.getDepartments()
    },
    // 编辑
    hEdit(id) {
      // console.log(id)
      this.curId = id
      this.showDialog = true
      this.isEdit = true
    },
    // 添加
    hAdd(id) {
      // console.log(id)
      this.curId = id
      this.showDialog = true
      this.isEdit = false
    },
    // 查询部门详情
    async getDepartments() {
      const res = await getDepartments()
      // console.log(res)
      // 把数据保存到数组中
      this.list = res.data.depts
      // 删除传智第一条数据
      res.data.depts.shift()
      // 把数据转成树形结构
      this.list = tranListToTreeData(res.data.depts)
    }
  }
}
</script>
